const path = require('path');
const express = require('express');
const app = express();

require('express-async-errors');
require('dotenv').config();
const errCaptureMiddleware = require('./middleware/err-capture');

const fileUpload = require('express-fileupload');
app.use(fileUpload());

app.use(express.static(path.resolve(__dirname, 'public')));
app.use(express.json());

// 上传路由
const { uploadProductImage } = require('./route-handlers/uploads');
app.post('/upload', uploadProductImage);
app.get('*', (req, res) => {
   res.sendFile(path.resolve(__dirname, 'public', 'index.html'));
});

app.use(errCaptureMiddleware); // 全局错误捕获

app.listen(8080, () => {
   console.log('Server is running on port 8080');
});